package org.shana.shortlink.admin.controller;

import lombok.RequiredArgsConstructor;
import org.shana.shortlink.admin.common.convention.result.Result;
import org.shana.shortlink.admin.common.convention.result.Results;
import org.shana.shortlink.admin.dto.req.ShortLinkGroupSaveReqDTO;
import org.shana.shortlink.admin.dto.req.ShortLinkGroupSortReqDTO;
import org.shana.shortlink.admin.dto.req.ShortLinkGroupUpdateReqDTO;
import org.shana.shortlink.admin.dto.resp.ShortLinkGroupRespDTO;
import org.shana.shortlink.admin.service.GroupService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 短链接分组控制层
 */
@RestController
@RequestMapping("/api/v1/short-link/admin/group")
@RequiredArgsConstructor
public class GroupController {

    private final GroupService groupService;

    @PostMapping("/save")
    public Result<String> save(@RequestBody ShortLinkGroupSaveReqDTO requestParam) {
        groupService.saveGroup(requestParam.getName());
        return Results.success("新增分组成功");
    }

    @GetMapping("/list")
    public Result<List<ShortLinkGroupRespDTO>> listGroup() {
        return Results.success(groupService.listGroup());
    }

    /**
     * 修改短链接分组名称
     */
    @PutMapping("/update")
    public Result<String> updateGroup(@RequestBody ShortLinkGroupUpdateReqDTO requestParam) {
        groupService.updateGroup(requestParam);
        return Results.success("修改短链接分组成功");
    }


    /**
     * 删除短链接分组
     */
    @DeleteMapping("/delete")
    public Result<String> updateGroup(@RequestParam String gid) {

        return Results.success(groupService.deleteGroup(gid));
    }

    /**
     * 删除短链接分组
     */
    @PostMapping("/sort")
    public Result<String> sortGroup(@RequestBody List<ShortLinkGroupSortReqDTO> requestParam) {
        groupService.sortGroup(requestParam);
        return Results.success("排序成功");
    }
}
